package cn.iocoder.yudao.module.ao.controller.admin.department;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ao.controller.admin.department.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.department.DepartmentDO;
import cn.iocoder.yudao.module.ao.service.department.DepartmentService;

@Tag(name = "管理后台 - 院系信息")
@RestController
@RequestMapping("/ao/department")
@Validated
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    @PostMapping("/create")
    @Operation(summary = "创建院系信息")
    @PreAuthorize("@ss.hasPermission('ao:department:create')")
    public CommonResult<Long> createDepartment(@Valid @RequestBody DepartmentSaveReqVO createReqVO) {
        return success(departmentService.createDepartment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新院系信息")
    @PreAuthorize("@ss.hasPermission('ao:department:update')")
    public CommonResult<Boolean> updateDepartment(@Valid @RequestBody DepartmentSaveReqVO updateReqVO) {
        departmentService.updateDepartment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除院系信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ao:department:delete')")
    public CommonResult<Boolean> deleteDepartment(@RequestParam("id") Long id) {
        departmentService.deleteDepartment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除院系信息")
                @PreAuthorize("@ss.hasPermission('ao:department:delete')")
    public CommonResult<Boolean> deleteDepartmentList(@RequestParam("ids") List<Long> ids) {
        departmentService.deleteDepartmentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得院系信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ao:department:query')")
    public CommonResult<DepartmentRespVO> getDepartment(@RequestParam("id") Long id) {
        DepartmentDO department = departmentService.getDepartment(id);
        return success(BeanUtils.toBean(department, DepartmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得院系信息分页")
    @PreAuthorize("@ss.hasPermission('ao:department:query')")
    public CommonResult<PageResult<DepartmentRespVO>> getDepartmentPage(@Valid DepartmentPageReqVO pageReqVO) {
        PageResult<DepartmentDO> pageResult = departmentService.getDepartmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepartmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出院系信息 Excel")
    @PreAuthorize("@ss.hasPermission('ao:department:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDepartmentExcel(@Valid DepartmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepartmentDO> list = departmentService.getDepartmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "院系信息.xls", "数据", DepartmentRespVO.class,
                        BeanUtils.toBean(list, DepartmentRespVO.class));
    }

}